package com.krzysztofadamczyk.dddex.domain.events;

import java.util.ArrayList;
import java.util.List;


public class DomainEvents {

    private static final List<EventHandler> persistenceHandlers = new ArrayList<EventHandler>();
    private static final List<EventHandler> domainHandlers = new ArrayList<EventHandler>();

    @SuppressWarnings("unchecked")
    public static void publish(DomainEvent event) {
        System.out.println("Published event: " + event);
        for (EventHandler handler : persistenceHandlers) {
            if (handler.canHandle(event)) {
                handler.handleEvent(event);
            }
        }

        for (EventHandler handler : domainHandlers) {
            if (handler.canHandle(event)) {
                handler.handleEvent(event);
            }
        }
    }

    public static void registerHandler(EventHandler<?> handler) {
        domainHandlers.add(handler);
    }

    public static void registerPersistenceHandler(EventHandler<?> handler) {
        persistenceHandlers.add(handler);
    }
}
